const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
  //打包模式
  mode: 'development', //开发环境
  //入口文件
  entry: './src/index.tsx',
  //出口文件
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  //路径解析别名
  resolve: {
    extensions: ['.ts', '.tsx', '.js', 'jsx'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  //插件
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html',
    }),
  ],
  devServer: {
    // static: {
    //   directory: path.join(__dirname, 'public'),
    // },
    host: '127.0.0.1', //域名
    port: 8080, //端口号
    open: true, //自动打开浏览器
    hot: true, //热更新
    compress: true, //开启服务器端的GZIP压缩
  },
  module: {
    rules: [
      {
        test: /\.less$/i,
        use: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader'],
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        options: {
          transpileOnly: true,
        },
        exclude: /node_modules/,
      },
      {
        test: /.(png|jpe?g|gif|webp|svg)$/,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            // 小于200kb图片进行转换base64
            // 优点减少请求
            maxSize: 250 * 1024,
          },
        },
      },
    ],
  },
};
